// import java.util.*;

public class ListNode 
{
    int val;
    ListNode next = null;
    public ListNode (int val)
    {
        this.val = val;
    } 
}

class Solution
{
    public ListNode reverselist (ListNode head)
    {
        // 如果是空链表
        if (head == null)
            return null;
        // 通过前后指针pre和temp记录位置，当前指针反转后可以继续下一步
        ListNode pre = null; //前指针,记录修改后next
        ListNode now = head; //当前指针
        while(now != null)
        {
            ListNode temp = now.next; //后指针，记录修改前next
            now.next = pre; //反转后next
            pre = now; //pre更新
            now = temp; //now更新
        }
        return pre; //while结束now==null,pre为上一步now
    }
}